package com.gzc.dao.imp;

import java.util.List;

import org.hibernate.transform.Transformers;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.gzc.dao.IUserDao;
import com.gzc.model.User;

@Repository("userDao")
@SuppressWarnings("unchecked")
public class UserDao extends BaseDao<User> implements IUserDao {
	
	/** 通过一组用户ID，查询出一组用户对象 */	
	@Override
	public List<User> listByIds(Integer[] userIds){
		final String hql = "select new User(u.id) from User u where u.id in (:ids)";		//select u from User u left join fetch u.department where u.id in (:ids)
		Query q = this.getSession().createQuery(hql);
		q.setParameterList("ids", userIds);
		return q.list();
	}

	@Override
	public List<User> listAllCanSendUser(int userId) {
		String sql = "select t3.id, t3.nickname from t_user as t1 left join t_dep_scope as t2 on(t1.dep_id=t2.dep_id)" +
				" right join t_user as t3 on(t2.s_dep_id=t3.dep_id) where t1.id=?";
		//addEntity会自动将对象的关联值设置进去，但是此处对于department而言，没有意义设置进去，所以可以直接通过非管理来操作
//		return this.getSession().createSQLQuery(sql)
//					.addEntity(User.class).setParameter(0, userId).list();
		return this.getSession().createSQLQuery(sql)
					.setResultTransformer(Transformers.aliasToBean(User.class)).setParameter(0, userId).list();
	}
	
}
